package com.ken.app_manager.core;

import com.ken.app_manager.base.OnBaseCallback;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JdbcTemplate {
    /**
     * 查询处理
     *
     * @param preCreator      创建预处理对象
     * @param callbackHandler 创建语句对象
     */
    public void query(PreparedStatementCreator preCreator,
                      RowCallbackHandler callbackHandler,OnBaseCallback callback) throws DataAccessException {

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            try {
                connection = DBHelp.getConnection();
            } catch (ClassNotFoundException e) {
//                e.printStackTrace();
                throw new DataAccessException("加载驱动失败",callback, e);
            }

            preparedStatement = preCreator.createPreparedStatement(connection);
            resultSet = preparedStatement.executeQuery();

            /**
             * 遍历结果集
             * */
            while (resultSet.next()) {
                callbackHandler.processRow(resultSet);
            }
        } catch (SQLException e) {
            throw new DataAccessException("数据库链接失败！",callback, e);
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
//                    e.printStackTrace();
                    throw new DataAccessException("JdbcTemplate的关闭连接对象失败", callback,e);
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
//                    e.printStackTrace();
                    throw new DataAccessException("JdbcTemplate的关闭预处理对象失败",callback, e);
                }
            }

            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
//                    e.printStackTrace();
                    throw new DataAccessException("JdbcTemplate的关闭结果集对象失败",callback, e);
                }

            }
        }

    }

    /**
     * 增、删、改操作
     */
    public void update(PreparedStatementCreator preCreator, OnBaseCallback callback) throws DataAccessException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            try {
                connection = DBHelp.getConnection();
            } catch (ClassNotFoundException e) {
//                e.printStackTrace();
                throw new DataAccessException("加载驱动失败",callback, e);
            }

            preparedStatement = preCreator.createPreparedStatement(connection);
            int count = preparedStatement.executeUpdate();
            callback.onSuccess("数据插入成功");
            System.out.println("插入数据条数" + count);
        } catch (SQLException e) {
            throw new DataAccessException("JdbcTemplate的SQLException",callback, e);
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
//                    e.printStackTrace();
//                    callback.onError(500,"JdbcTemplate的关闭连接对象失败");
                    throw new DataAccessException("JdbcTemplate的关闭连接对象失败",callback, e);
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
//                    e.printStackTrace();
                    throw new DataAccessException("JdbcTemplate的关闭预处理对象失败",callback, e);
                }
            }
        }
    }
}
